﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Xml.Serialization;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public class HelpCommand : Command, ICommandInfo
    {
        public override string HelpText { get { return "Get a list of commands or help on a specific command"; } }

        [NonSerialized, XmlIgnore]
        public static List<string> Words = new List<string>() { "HELP" };

        public override List<string> FirstWords { get { return Words; } }

        [MethodVisible]
        public static List<PerceivedEventAndAudience> PerformMethod(CommandToPerform cmd, IGameThing source, IGameThing target, PropertyData args, PropertyData modifyingArguments)
        {
            List<PerceivedEventAndAudience> events = new List<PerceivedEventAndAudience>();
            GameChar sourceChar = source as GameChar;
            string allParams = (cmd.UserInput == null) ? string.Empty : cmd.UserInput;
            List<Command> commands = sourceChar.Abilities.EnabledCommands;
            List<HelpEntry> helps = new List<HelpEntry>();

            foreach (var command in commands)
            {
                if (!string.IsNullOrEmpty(allParams) && command.FirstWords.IndexOf(allParams) < 0)
                    continue;

                HelpEntry entry = HelpEntry.CreateFromCommand(command);
                helps.Add(entry);                
            }

            string output = helps.BuildList("\n", (h) => h.ToString());
            
            EventToPerceive evt = CreateObservable(source, output);
            events.Add(new PerceivedEventAndAudience() { Event = evt, Audience = new AssociatedIDCollection(sourceChar.AssociatedID) });

            return events;
        }

        [MethodVisible]
        public static EventToPerceive CreateObservable(IAssociatedID source, string text)
        {
            return new GeneralEventToPerceive()
            {
                ShowToSource = new Event(string.Format("Help/Commands: {0}{1}", Environment.NewLine, text), ViewType.Sense),
                Sources = new AssociatedIDCollection(source.AssociatedID)
            };
        }
    }
}
